package com.lzx.iteam;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import com.lzx.iteam.adapter.GuidePageAdapter;
import com.lzx.iteam.util.Constants;
import com.lzx.iteam.util.PreferenceUtil;
import com.umeng.analytics.MobclickAgent;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by hfx on 14-9-28.
 */
public class GuideActivity extends Activity implements ViewPager.OnPageChangeListener, View.OnClickListener {
    private ViewPager mWelcomePager;
    private Button mBtnStart;
    private LayoutInflater mLayoutInflater;
    private GuidePageAdapter mGuidePageAdapter;
    private int mListSize;
    private List<View> list;
    private LinearLayout mContainerSelect;
    private ImageView[] mImageViews;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mLayoutInflater = LayoutInflater.from(this);
        setContentView(R.layout.guide_page_layout);
        initViewPager();
    }


    @Override
    protected void onResume() {
        super.onResume();
        MobclickAgent.onResume(this);
    }

    private void initViewPager() {
        mWelcomePager = (ViewPager) findViewById(R.id.container_viewpage);
        mContainerSelect = (LinearLayout) findViewById(R.id.viewGroup);
        mBtnStart = (Button) findViewById(R.id.btn_guide_start);
        mBtnStart.setVisibility(View.GONE);
        mBtnStart.setOnClickListener(this);
        list = new ArrayList<View>();
        list.add(getPageByResrouce(R.drawable.guide_page_1));
        list.add(getPageByResrouce(R.drawable.guide_page_2));
        list.add(getPageByResrouce(R.drawable.guide_page_3));
        list.add(getPageByResrouce(R.drawable.guide_page_4));
        list.add(getPageByResrouce(R.drawable.guide_page_5));
        mListSize = list.size();
        mImageViews = new ImageView[mListSize];
        for (int i = 0; i < list.size(); i++) {
            ImageView imageView = new ImageView(GuideActivity.this);
            LinearLayout.LayoutParams vl = new LinearLayout.LayoutParams(Constants.dip2px(this,6), Constants.dip2px(this,6));
            vl.setMargins(9, 0, 9, 0);
            imageView.setLayoutParams(vl);
            mImageViews[i] = imageView;
            if (i == 0) {
                mImageViews[i].setBackgroundResource(R.drawable.guide_dot_selected);
            } else {
                mImageViews[i].setBackgroundResource(R.drawable.guide_dot_normal);
            }
            mContainerSelect.addView(mImageViews[i]);
        }
        mGuidePageAdapter = new GuidePageAdapter(list);
        mWelcomePager.setAdapter(mGuidePageAdapter);
        mWelcomePager.setCurrentItem(0);
        mWelcomePager.setOnPageChangeListener(this);
    }

    private View getPageByResrouce(int dawable) {
        ImageView result = (ImageView) mLayoutInflater.inflate(R.layout.item_guide_page, null);
        result.setImageResource(dawable);
        return result;
    }

    @Override
    public void onPageScrolled(int i, float v, int i2) {

    }

    @Override
    public void onPageSelected(int i) {
        for (int j = 0; j < mImageViews.length; j++) {
            if (i != j) {
                mImageViews[j].setBackgroundResource(R.drawable.guide_dot_normal);
            }else{
                mImageViews[i].setBackgroundResource(R.drawable.guide_dot_selected);
            }
        }
        if(i + 1 == mListSize){
            mBtnStart.setVisibility(View.VISIBLE);
            mContainerSelect.setVisibility(View.GONE);
        }else{
            mBtnStart.setVisibility(View.GONE);
            mContainerSelect.setVisibility(View.VISIBLE);
        }
    }

    @Override
    public void onPageScrollStateChanged(int i) {

    }


    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.btn_guide_start:
                PreferenceUtil.getInstance(this).save(PreferenceUtil.IS_FIRST_START, false);
                Intent intent = new Intent(this,MainActivity.class);
                startActivity(intent);
                finish();
                break;
        }
    }

    @Override
    protected void onDestroy() {
        mWelcomePager = null;
        mGuidePageAdapter =  null;
        list = null;
        System.gc();
        super.onDestroy();
    }
}